package info.motteke.mixivoice4j;


/**
 * コメントを作成するためのクラスです。
 *
 * @author Y.Wakamatsu
 *
 */
public class CommentBuilder {

    private final MixiVoiceCache cache;

    private final MixiVoiceObserver observer;

    private long id;

    private User user;

    private Body body;

    private Entry parent;

    /* package */ CommentBuilder(MixiVoiceCache cache, MixiVoiceObserver observer) {
        this.cache = cache;
        this.observer = observer;
    }

    public CommentBuilder identifiedBy(long id, User owner) {
        this.id = id;
        this.user = owner;

        return this;
    }

    public CommentBuilder body(Body body) {
        this.body = body;

        return this;
    }

    public CommentBuilder parent (Entry parent) {
        this.parent = parent;

        return this;
    }

    public Comment build() {
        Comment c = cache.getVoice(id, user, Comment.class);

        if (c == null) {
            c = new Comment(this);
            cache.addVoice(c);
            observer.addNewVoice(c);
            observer.addUpdatedEntry(parent);
        }

        parent.addComment(c);
        return c;
    }

    /* package */ Comment build(Entry parent) {
        this.parent = parent;
        return build();
    }

    /* package */ long getId() {
        return id;
    }

    /* package */ User getUser() {
        return user;
    }

    /* package */ Body getBody() {
        return body;
    }

    /* package */ Entry getParent() {
        return parent;
    }
}
